public class EditPayoutVF
{
	private final Plan__c[] plan = new Plan__c[1];
    
    public Plan__c planObj {get; set;}
    public Rate__c rateObj {get; set;}
    public String rateType {get; set;}
    
    public Boolean displayAttainmentTable {get; set;}
    public Boolean displayCreditTable {get; set;}
	public Boolean deleteTierFlag {get; set;}
	public Boolean rateTypeFlag {get; set;}
	public Boolean showAttainmentSymbol {get; set;} 
	public String attainmentSymbol {get{ attainmentSymbol = '%'; return attainmentSymbol;} set;}
	
    public List<AttainmentWrapper> payoutAttainmentList {get; set;}
    public List<CreditWrapper> payoutCreditList {get; set;}
    private List<AttainmentWrapper> attainmentList = new List<AttainmentWrapper>();
    private List<CreditWrapper> creditList = new List<CreditWrapper>();
    
    public EditPayoutVF(ApexPages.StandardController myController)
    {
        plan[0] = (Plan__c)myController.getRecord();
        planObj = [select id, Quota__c, OTE__c from Plan__c where id = :plan[0].id];

        displayAttainmentTable = true;
        displayCreditTable = false;
        rateTypeFlag = false;
        deleteTierFlag = true;
        
        if(payoutAttainmentList == null)
            payoutAttainmentList = new List<AttainmentWrapper>();
        if(payoutCreditList == null)
            payoutCreditList = new List<CreditWrapper>();
        
        Rate__c[] rateList = [select Min_Attainment__c, Max_Attainment__c, Min_Credit__c, Max_Credit__c, Rate_Type__c, Rate__c from Rate__c where Plan__c = :plan[0].id];
        if(rateList.size() > 0)
        {
            rateType = rateList[0].Rate_Type__c;
            rateTypeFlag = !rateTypeFlag;
            if(rateList[0].Rate_Type__c == 'Credit')
            {
                for(Rate__c r: rateList)
                {
                	CreditWrapper c =  new CreditWrapper(String.valueOf(r.Min_Credit__c), String.valueOf(r.Max_Credit__c), String.valueOf(r.Rate__c), true);
                    payoutCreditList.add(c);
                }
                displayAttainmentTable = !displayAttainmentTable;
                displayCreditTable = !displayCreditTable;
                
                if(payoutCreditList.size() > 1)
                	deleteTierFlag = !deleteTierFlag;
                
            }
            else
            {
                for(Rate__c r: rateList)
                {
                	AttainmentWrapper a = new AttainmentWrapper(String.valueOf(r.Min_Attainment__c), String.valueOf(r.Max_Attainment__c), String.valueOf(r.Rate__c), true);
                    payoutAttainmentList.add(a);
                }
                if(payoutAttainmentList.size() > 1)
                	deleteTierFlag = !deleteTierFlag;
            }
        }
        else
        {
            payoutAttainmentList.add(new AttainmentWrapper(String.valueOf(0), String.valueOf(0), String.valueOf(0), true));
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, ''+rateList.size()+' -- '+payoutAttainmentList.size()));         
        }
        
        if(displayAttainmentTable)
        	showAttainmentSymbol = true;
    }
    
    public List<SelectOption> getRateTypeList()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Attainment', 'Attainment'));
        options.add(new SelectOption('Credit', 'Credit'));
        return options;
    }
    
    public PageReference addTier()
    {
        if(rateType == 'Attainment')
        {
        	String adjVal = payoutAttainmentList.get(payoutAttainmentList.size() - 1).Max;
            payoutAttainmentList.add(new AttainmentWrapper(adjVal, adjVal, payoutAttainmentList.get(payoutAttainmentList.size() - 1).Rate, true));
        }
        else
        {
        	String adjVal = payoutCreditList.get(payoutCreditList.size() - 1).Max;
            payoutCreditList.add(new CreditWrapper(adjVal, adjVal, payoutCreditList.get(payoutCreditList.size() - 1).Rate, true));
        }
            
        if(deleteTierFlag)
        	deleteTierFlag = !deleteTierFlag;
        return null;
    }
    
    public PageReference deleteTier()
    {
        if(rateType == 'Attainment' && payoutAttainmentList.size() > 1)
            payoutAttainmentList.remove(payoutAttainmentList.size() - 1);
        else if(payoutCreditList.size() > 1)
            payoutCreditList.remove(payoutCreditList.size() - 1);
            
        if(payoutAttainmentList.size() == 1 || payoutCreditList.size() == 1 && !deleteTierFlag)
        	deleteTierFlag = !deleteTierFlag;
        return null;
    }
    
    public class AttainmentWrapper
    {
        public String min {get; set;}
        public String max {get; set;}
        public String Rate {get; set;}
        public Boolean disableAttainmentFlag {get; set;}
        public AttainmentWrapper(String minimum, String maximum, String R, Boolean flag)
        {
            min = minimum;
            max = maximum;
            Rate = R;
            disableAttainmentFlag = flag;
        }
    }
    
    public class CreditWrapper
    {
        public String min {get; set;}
        public String max {get; set;}
        public String Rate {get; set;}
        public Boolean disableCreditFlag {get; set;}
        public CreditWrapper(String minimum, String maximum, String R, Boolean flag)
        {
            min = minimum;
            max = maximum;
            Rate = R;
            disableCreditFlag = flag;
        }

    }

    public List<AttainmentWrapper> getAttainmentRows() {
        return payoutAttainmentList;
    }
    
    public List<CreditWrapper> getCreditRows() {
        return payoutCreditList;
    }

    public PageReference checkSelection()
    {
        displayAttainmentTable = !displayAttainmentTable;
        displayCreditTable = !displayCreditTable;
        if(rateType == 'Attainment')
        {
            for(CreditWrapper aw: payoutCreditList)
                creditList.add(aw);
            payoutCreditList.clear();
            if(attainmentList.size() > 0)
            {
                for(AttainmentWrapper aw: attainmentList)
                    payoutAttainmentList.add(aw);
                attainmentList.clear();
            }
        	if(payoutAttainmentList.size() <= 1)
        		deleteTierFlag = true;
        	else
        		deleteTierFlag = false;
        }
        else
        {
            for(AttainmentWrapper aw: payoutAttainmentList)
                attainmentList.add(aw);
            payoutAttainmentList.clear();                    

            if(creditList.size() == 0 && payoutCreditList.size() == 0)
            {
                payoutCreditList.add(new CreditWrapper(String.valueOf(0), String.valueOf(0), String.valueOf(0), true));
            }
            
            if(creditList.size() > 0)
            {
                for(CreditWrapper aw: creditList)
                    payoutCreditList.add(aw);
                creditList.clear();
            }
            
        	if(payoutCreditList.size() <= 1)
        		deleteTierFlag = true;
        	else
        		deleteTierFlag = false;
        }
        
        if(displayAttainmentTable)
        	showAttainmentSymbol = true;
        else
        	showAttainmentSymbol = false;
        return null;
    }    

    public PageReference save()
    {
        for(Rate__c[] rList : [select id from Rate__c where Plan__c = :plan[0].id])
            delete rList;
            
        Rate__c[] rateList = new Rate__c[]{};
        update planObj;
                
        if(rateType == 'Attainment')
        {
            for(AttainmentWrapper aw: payoutAttainmentList)
            {
                if(Double.valueOf(aw.min) >= 0 && Double.valueOf(aw.max) > 0 && Double.valueOf(aw.Rate) > 0)
                {
                    Rate__c rate = new Rate__c(Plan__c = plan[0].Id, Min_Attainment__c = Double.valueOf(aw.min), Max_Attainment__c = Double.valueOf(aw.max), Rate__c = Double.valueOf(aw.Rate), Rate_Type__c = rateType);
                    rateList.add(rate);                    
                }      
                //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, ''+rateList.size()));         
            }
        }
        else
        {
            for(CreditWrapper cw: payoutCreditList)
            {
                if(Double.valueOf(cw.min) >= 0 && Double.valueOf(cw.max) > 0 && Double.valueOf(cw.Rate) > 0)
                {
                    Rate__c rate = new Rate__c(Plan__c = plan[0].Id, Min_Credit__c = Double.valueOf(cw.min), Max_Credit__c = Double.valueOf(cw.max), Rate__c = Double.valueOf(cw.Rate), Rate_Type__c = rateType);
                    rateList.add(rate);                    
                }               
            }
        }
        if(rateList.size() > 0)
            insert rateList;
        
        return null;
    }
    
}